<template>
  <div class="user-layout-wrapper">
    <div class="container">
      <div class="top">
        <div class="header">
          <span class="title">{{ title }}</span>
          <!-- <span class="desc">V1.0</span> -->
        </div>
      </div>
      <div class="main" id="main">
        <div class="front" id="front">
          <el-form class="user-layout-login" ref="formLogin" :model="createForm" :rules="rules" label-width="80px">
            <el-form-item prop="loginName" label="用户名" v-if="!clickRpaswd">
              <el-input size="large" prefix-icon="el-icon-phone-outline" v-model.trim="createForm.loginName" type="text"
                placeholder="请输入用户名" />
            </el-form-item>

            <!-- 忘记密码 -->
            <el-form-item prop="phone" label="手机号" v-if="clickRpaswd">
                <el-input size="large" prefix-icon="el-icon-phone-outline" v-model.trim="createForm.phone" type="text"
                placeholder="请输入手机号" />
            </el-form-item>

            <el-form-item prop="password" style="margin-top: 24px;" label="密码">
              <el-input size="large" prefix-icon="el-icon-lock" v-model.trim="createForm.password" type="password"
                placeholder="请输入密码" show-password>
              </el-input>
            </el-form-item>

            <el-form-item prop="password" style="margin-top: 24px;" label="确认密码" v-if="clickRpaswd">
              <el-input size="large" prefix-icon="el-icon-lock" v-model.trim="createForm.password" type="password"
                placeholder="请确认密码" show-password>
              </el-input>
            </el-form-item>

            <el-form-item prop="verificationCode" label="短信验证" class="verificationCodeClass">
                <el-input size="large" prefix-icon="el-icon-user" v-model.trim="createForm.verificationCode" type="text" placeholder="短信验证码" />
                <el-button @click.prevent="removeDomain(domain)" type="primary">发送验证码</el-button>
            </el-form-item>
            <el-form-item style="margin-top:24px">
              <el-button size="large" type="primary" htmlType="submit" class="login-button" :loading="loginBtn"
                @click.stop.prevent="handleSubmit('formLogin')" :disabled="loginBtn">{{ clickRpaswd ? '提交' : '登录' }}</el-button>
            </el-form-item>
            <el-form-item style="margin-top:24px">
              <div class="register">
                <span  @click="flipCard('front')">没有账号?去注册</span>
                <span v-if="clickRpaswd" @click="clickRpaswd = false" style="margin-right: 20px;">登录</span>
                <span v-if="!clickRpaswd" @click="bindRetrievePassword" style="margin-right: 20px;">忘记密码</span>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="back" id="back" >
          <el-tabs v-model="editableTabsValue"  stretch tab-position="left">
            <el-tab-pane
              v-for="item in editableTabs"
              :key="item.name"
              :label="item.title"
              :name="item.name"
            >
            </el-tab-pane>
            <ComReg :isCompany="editableTabsValue"></ComReg>
          </el-tabs>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions } from "vuex"
import Vue from 'vue'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import defaultSettings from '@/settings'
import ComReg from './reg.vue'
import ComRpswd from './retrievePassword.vue'

export default {
  components: { ComReg, ComRpswd },
  data () {
    var validatePhone = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('请输入手机号'));
            }
            const reg = /^1[3-9]\d{9}$/;
            if (!reg.test(value)) {
                return callback(new Error('请输入正确的手机号'));
            }
            callback();
    }
    return {
      title: defaultSettings.title,
      loginBtn: false,
      clickRpaswd: false,
      createForm: {
        phone: "",
        password: ""
      },
      editableTabsValue: '1',
      editableTabs: [{
        title: '个人注册',
        name: '1',
      }, {
        title: '企业注册',
        name: '2',
      }],
      rules: {
        // phone: [
        //     { required: true, validator: validatePhone, trigger: 'blur' },
        // ],
        loginName: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          { max: 20, message: "字数不超过20", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { max: 20, message: "字数不超过20", trigger: "blur" },
        ],
        // verificationCode: [
        //     { required: true, message: "请输入短信验证码", trigger: "blur" },
        // ],
      },
    }
  },
  created () {
    this.currdatetime = new Date().getTime();
    Vue.ls.remove(ACCESS_TOKEN);
  },
  methods: {
    ...mapActions(["Login"]),
    flipCard(id){
      const doc_front = document.getElementById('front');
      const doc_back = document.getElementById('back');
      const doc_main = document.getElementById('main');
      if(id === 'front'){
        doc_front.classList.add('opty0');
        doc_front.classList.remove('opty1');
        doc_back.classList.add('opty1');
        doc_back.classList.remove('opty0');
        doc_main.classList.add('mainBack')
      }else{
        doc_front.classList.add('opty1');
        doc_front.classList.remove('opty0');
        doc_back.classList.add('opty0');
        doc_back.classList.remove('opty1');
        doc_main.classList.remove('mainBack')
      }
      this.resetForm('formLogin')
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSubmit (formData) {
      let that = this
      this.$refs[formData].validate((valid) => {
        if (valid) {
          let str = "(" + this.createForm.loginName + ")|(" + this.createForm.password + ")";
          str = Sm3Utils.encryptFromText(str);
          let encData = new Sm2Utils().encryptFromText(defaultSettings.publicKey, this.createForm.loginName + "|" + this.createForm.password + "|" + new Date().getTime() + "|" + str);
          let params = {
            encData: encData
          }
          this.Login(params).then((res) => {
            const data = res.data
            if (data.code == 0) {
              this.loginSuccess(data)
            } else {
              this.requestFailed(data)
            }
          })
        } else {
          that.loginBtn = false;
        }
      });
    },
    loginSuccess (data) {
      this.$router.push({ path: "/res/standbook" })
      this.$notify({
        title: '欢迎',
        message: `${data.data.user.username}，欢迎回来`,
        type: 'success'
      });
    },
    requestFailed (err) {
      this.$notify({
        title: '登录失败',
        message: err.data || "请求出现错误，请稍后再试",
        type: 'error'
      });
      this.loginBtn = false;
    },
    bindRetrievePassword(){
      this.clickRpaswd = true;
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .verificationCodeClass{
    .el-form-item__content{
        display: flex;
        .el-button{
            margin-left: 30px;
        }
    }
}
.main{
  position: relative;
  .front, .back{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    transition: opacity 0.5s ease-in-out;
    opacity: 1; 
    z-index: 10000;
    height: 800px;
    overflow: scroll;
  }
  .back{
    opacity: 0; 
    z-index: 0;
  }
  .opty1{
    opacity: 1; 
    z-index: 100000;
  }
  .opty0{
    opacity: 0;
    z-index: 0;
    width: 0;
  }

}

.register{
  display: flex;
  flex-direction:row-reverse;
  color: #3F9EFF;
  cursor: pointer;
  > span{
    position: relative;
    text-decoration: none;
    &::after{
    content: '';
    position: absolute;
    bottom: -2px;
    right: 0;
    width: 0;
    height: 2px;
    background-color: #3F9EFF;
    transition: width 0.2s ease-in-out;
  }
    &:hover::after {
      width: 100%;
    }
  }
}
.user-layout-wrapper {
  height: 100%;
  background: #fff;

  .container {
    width: 100%;
    min-height: 100%;
    background-size: 100%;
    padding: 110px 0 144px;
    position: relative;

    .top {
      text-align: center;

      .header {
        height: 44px;
        line-height: 44px;
        margin-bottom: 45px;

        .title {
          font-size: 35px;
          color: #666;
          font-family: "Chinese Quote", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
          font-weight: 700;
          position: relative;
          top: 2px;
        }

        .desc {
          font-size: 16px;
          color: #666;
          margin-top: 12px;
          margin-left: 10px;
          margin-bottom: 40px;
        }
      }
    }

    .main {
      // min-width: 260px;
      width: 450px;
      margin: 0 auto;
    }
    .mainBack {
      width: 900px;
    }
  }
}

.user-layout-login {

  .forge-password {
    font-size: 14px;
    font-weight: bolder;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }
}
</style>